package www.sofn.com.sys.user;

import cn.hutool.core.bean.BeanUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import www.sofn.com.domain.R;
import www.sofn.com.domain.vo.PageRestVO;
import www.sofn.com.sys.user.domain.pojo.SysUserPO;
import www.sofn.com.sys.user.domain.query.SysUserQuery;
import www.sofn.com.sys.user.domain.vo.ListSysUserVO;
import www.sofn.com.sys.user.domain.vo.SysUserVO;
import www.sofn.com.validate.group.InsertGroup;
import www.sofn.com.validate.group.UpdateGroup;

@RestController
@RequestMapping("sys_user")
@Tag( name = "系统用户")
public class SysUserController {
    @Resource
    private SysUserService sysUserService;

    @GetMapping
    @Operation(summary = "查询用户列表")
    @PreAuthorize("hasAuthority('sys:user:select')")
    public  R<PageRestVO<ListSysUserVO>>  pageList(SysUserQuery query) {
        return R.ok(sysUserService.pageList(query));
    }

    @GetMapping("/{id}")
    @Operation(summary = "获取用户详情")
    @PreAuthorize("hasAuthority('sys:user:select')")
    public R<SysUserVO> getById(@PathVariable(value="id" ,required = true) String id){
        return R.ok(  sysUserService.getById(id) );
    }
    @Operation(summary = "新增用户")
    @PostMapping
    @PreAuthorize("hasAuthority('sys:user:create')")
    public R insert(@RequestBody @Validated(value = {InsertGroup.class}) SysUserPO userVO) {
        sysUserService.save(BeanUtil.toBean(userVO, SysUserPO.class));
        return R.ok();
    }
    @Operation(summary = "修改用户")
    @PutMapping
    @PreAuthorize("hasAuthority('sys:user:update')")
    public R update(@RequestBody @Validated(value = {UpdateGroup.class}) SysUserPO userVO) {
        sysUserService.update(BeanUtil.toBean(userVO, SysUserPO.class));
        return R.ok();
    }

    @Operation(summary = "删除用户")
    @DeleteMapping("/{id}")
    @PreAuthorize("hasAuthority('sys:user:delete')")
    public R delete(@PathVariable(value = "id", required = true) String id) {
        sysUserService.deleteById(id);
        return R.ok();
    }

}
